package com.hhkj.study.admin.config.service.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.hhkj.study.admin.config.entity.ConfigBase;
import com.hhkj.study.admin.config.model.podo.ConfigBaseDo;
import com.hhkj.study.admin.config.model.podo.ConfigBasePo;
import com.hhkj.study.admin.config.model.req.ConfigBaseReq;
import com.hhkj.study.admin.config.model.res.ConfigBaseRes;
import com.hhkj.study.admin.config.service.base.ConfigBaseService;
import com.hhkj.study.admin.config.service.biz.ConfigBaseBizService;
import com.hhkj.study.admin.config.service.extend.ConfigBaseExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.hhkj.study.common.handler.CommonHandler;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* ConfigBaseBizServiceImpl
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class ConfigBaseBizServiceImpl extends BaseBizServiceImpl<ConfigBase, ConfigBaseReq, ConfigBaseRes, ConfigBaseDo, ConfigBasePo, ConfigBaseService, ConfigBaseExtendService> implements ConfigBaseBizService {

    @Resource
    private GlobalCache globalCache;

    @Override
    public boolean updateDisableFlag(ConfigBaseReq req) {
        ConfigBase configBase = this.getExtendService().getByIdNotNull(req.getId());
        if (configBase.getDisableFlag().equals(req.getDisableFlag())) {
            throw new GlobalException(SysConst.Flag.IS.getFlag().equals(req.getDisableFlag()) ?
                    "已禁用" : "已启用");
        }
        LambdaUpdateWrapper<ConfigBase> configBaseUpdate = new LambdaUpdateWrapper<>();
        configBaseUpdate.eq(ConfigBase::getId, req.getId())
                .set(ConfigBase::getDisableFlag, req.getDisableFlag());
        return this.getService().update(configBaseUpdate);
    }

    @Override
    protected void afterList(List<ConfigBaseRes> configBaseResList) {
        if (CollectionUtil.isEmpty(configBaseResList)) {
            return;
        }
        Map<Long, String> dictMap = globalCache.cacheDictMap(Lists.newArrayList(DictCodeEnum.COURSE_TYPE.getCode(), DictCodeEnum.REGION.getCode()));
        configBaseResList.forEach(configBase -> {
            configBase.setCourseTypeName(dictMap.get(configBase.getCourseType()));
            configBase.setProvinceMap(CommonHandler.getMapByDict(dictMap, configBase.getProvinceIdList()));
            configBase.setProvinceNames(CommonHandler.joinStr(dictMap, configBase.getProvinceIdList(), Constants.VERTICAL_LINE));
        });
        List<Long> topConfigBaseIdList = configBaseResList.stream().filter(e -> Constants.TOP_PARENT_ID.equals(e.getParentId())).map(ConfigBaseRes::getId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(topConfigBaseIdList)) {
            return;
        }
        LambdaQueryWrapper<ConfigBase> configBaseQuery = new LambdaQueryWrapper<>();
        configBaseQuery.in(ConfigBase::getParentId, topConfigBaseIdList).eq(ConfigBase::getDisableFlag, SysConst.Flag.NO.getFlag());
        List<ConfigBase> configBaseList = this.getService().list(configBaseQuery);
        if (CollectionUtil.isEmpty(configBaseList)) {
            return;
        }
        // 最顶层获取到下级配置值
        Map<Long, List<String>> topConfigMap = configBaseList.stream().collect(Collectors.groupingBy(ConfigBase::getParentId, Collectors.mapping(ConfigBase::getValue, Collectors.toList())));
        configBaseResList.forEach(configBase -> configBase.setValueList(topConfigMap.get(configBase.getId())));

    }
}